<?php

namespace App\Admin\Controllers;

use App\Models\Goods;
use App\Models\GoodsClassify;
use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class GoodsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品';
 
    protected $goodsStatus = [
        'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
        'off' => ['value' => 0, 'text' => '修整', 'color' => 'danger'],
    ];
    public function index(Content $content)
    {
	    return $content->description('这是一个分门店特有菜单，总后台不提供');
    }
	
	/**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Goods());

        $grid->column('name', __('Name'));
        $grid->column('images_url', __('Images url'))
            ->image('', '40');
        $grid->column('price', __('Price'));
        $grid->column('stock', __('Stock'));
        $grid->column('sell', __('Sell'));
        $grid->column('sort', __('Sort'));
        $grid->column('goods_number', __('Goods number'));
        $grid->column('status', __('Status'))->switch($this->goodsStatus);
        $grid->column('created_at', __('Created at'));

        // 筛选
        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name', __('Name'));
            $filter->equal('status', __('Status'))->radio(['' => '全部', 1 => '正常', 2 => '修整']);

            // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
            $filter->scope('trashed', '回收站')->onlyTrashed();
        });

        // 回收站恢复操作
        $grid->actions(function ($actions) {

            if (\request('_scope_') == 'trashed') {
                $actions->add(new Restore());
            }

        });
        $grid->batchActions(function ($batch) {

            if (\request('_scope_') == 'trashed') {
                $batch->add(new BatchRestore());
            }

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Goods::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('images_url', __('Images url'));
        $show->field('price', __('Price'));
        $show->field('stock', __('Stock'));
        $show->field('sell', __('Sell'));
        $show->field('sort', __('Sort'));
        $show->field('describe', __('Describe'));
        $show->field('goods_number', __('Goods number'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Goods());

        $form->text('name', __('Name'))->rules('required|max:100');

        $form->multipleSelect('classify', '分类')
            ->options(GoodsClassify::all()->pluck('title', 'id'))
            ->rules('required');

        $form->image('images_url', __('Images url'))
            ->removable()
            ->uniqueName()
            ->rules('required');

        $form->currency('price', __('Price'))->symbol('￥')
            ->rules('required|min:1');

        $form->number('stock', __('Stock'))
            ->rules('required|min:1');

        $form->number('sell', __('Sell'))
            ->rules('required|min:1');

        $form->number('sort', __('Sort'))
            ->rules('required|min:1');

        $form->editor('describe', __('Describe'));
        $form->text('goods_number', __('Goods number'))
            ->creationRules('required|unique:user_table')
            ->updateRules('required|unique:goods,goods_number,{{id}}');

        $form->switch('status', __('Status'))
            ->states($this->goodsStatus);

        return $form;
    }
}
